package example.demo.route.aop;

import example.demo.route.annotation.Route;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * 用这个的原因是Invocation invocation拿不到什么东东。就是拿不到方法上的注解。。。
 */
@Aspect
@Component
public class RouteAop {


    @Before("@annotation(route)")
    public void routeBefore(/*JoinPoint point,*/Route route) {
        int tables = route.tables();
        if (tables != 0) {
            Integer[] ints = new Integer[tables + 1];
            for (int i = 0; i <= tables; i++) {
                ints[i] = i;
            }
            RouteContext.tables_thread_local.set(ints);
        } else {
            int[] ints = route.tableIdxs();
            int len = ints.length;
            if (/*null != ints &&*/ len != 0) {
                Integer[] integers = new Integer[len];
                for (int i = 0; i < len; i++) {
                    integers[i] = ints[i];
                }
                RouteContext.tableIdxs_thread_local.set(integers);
            }
        }
    }
}
